//数量加减按钮
function changeCut(index, type){
	if(type == 1){
		orderdata.orderItems[index].product_quantity++
	}else{
		if(orderdata.orderItems[index].product_quantity > 1){
			orderdata.orderItems[index].product_quantity--
		}
	}
	orderdata.orderItems[index].product_price = orderdata.orderItems[index].unit_price*orderdata.orderItems[index].product_quantity
	
	
	/*$("#confirm-order-div").setTemplateURL('tmpl/confirmOrderTmpl.html');
	$("#confirm-order-div").setParam("width", window.screen.width); 
	$("#confirm-order-div").processTemplate(orderdata);*/
	$("#price"+index).text(orderdata.orderItems[index].product_price)
	$("#input-num"+index).text(orderdata.orderItems[index].product_quantity)
	$("#cnt"+index).text(orderdata.orderItems[index].product_quantity)
	
	//计算经过选择数量，优惠券、积分兑换后的金额
	onChangedPriceAll()
}

//买家留言信息
function onMsgTextChanged(index, t){
	orderdata.orderItems[index].message = $(t).val()
}

/**
 * 选择优惠信息
 * @param {Object} t  控件
 * @param {Object} price 满多少钱
 * @param {Object} deductible  减多少钱
 */
function onCupExchanged(t){
	var couponExchange_id = $(t).val()
	
	//分解
	var exchange = [0,0,0]
	if(couponExchange_id != "-1"){
		var temp = couponExchange_id.split('-')
		exchange[0] = temp[0]
		exchange[1] = temp[1]
		exchange[2] = temp[2]
	}
	
	orderdata.couponExchange_id = exchange[0]
	orderdata.couponExchange_price = exchange[1]
	orderdata.couponExchange_deductible = exchange[2]
	
	//计算经过选择数量，优惠券、积分兑换后的金额
	onChangedPriceAll()
}

//确认订单
function confirmOrder(){
	var para = {
		address_id: orderdata.shopReceiver.id,
		couponExchange_id: orderdata.couponExchange_id
		// 积分这个没有取到值
		/*deductible_integral: orderdata.integral*/
		
	}
	if(para.couponExchange_id == null || para.couponExchange_id == 0){
		para.couponExchange_id = -1
	}
	//积分
	if(orderdata.integral != null){
		para.deductible_integral = orderdata.integral
	}else{
		para.deductible_integral = 0
	}
	
	var product_sku = []
	for(var i=0;i<orderdata.orderItems.length;i++){
		var item = orderdata.orderItems[i]
		product_sku.push({product_id:item.product_id, sku_id:item.sku_id, quantity:item.product_quantity, message: item.message})
	}
	para.product_sku = product_sku
	ajax(api.submitOrder, {address_id:para.address_id	,
		couponExchange_id:para.couponExchange_id,
		deductible_integral:para.deductible_integral,
		product_sku:JSON.stringify(product_sku)}, function(data){
			window.location.href = data.url
		})
}

//积分输入变化
function onUserIntegralChanged(t){
	var v = $(t).val()
	var tag = parseInt($(t).attr("tag"))
	//判断最大积分
	if(v > tag){
		$(t).val(tag)
		toast("您的积分为:"+tag)
	}
	//和原始金额判断，是否达到减免要求
	orderdata.total_amount = 0
	for(var i=0;i<orderdata.orderItems.length;i++){
		orderdata.total_amount += orderdata.orderItems[i].product_price
	}
	
	//判断减免要求
	var max_price = 0
	var max_jianprice = 0
	
	for(var i=0;i<orderdata.integralDeductibles.length;i++){
		var item = orderdata.integralDeductibles[i]
		if(max_price < item.price && item.price <= orderdata.total_amount){
			max_price = item.price
			max_jianprice = item.max_price
		}
	}
	
	var max_integral = max_jianprice*1000
	if(v > max_integral){
		$(t).val(max_integral)
		toast("此商品您只能兑换"+max_integral)
	}
	
	orderdata.integral = $(t).val()
	//计算经过选择数量，优惠券、积分兑换后的金额
	onChangedPriceAll()
}

/**
 * 计算经过选择数量，优惠券、积分兑换后的金额
 */
function onChangedPriceAll(){
	//所有金额相加
	orderdata.total_amount = 0
	for(var i=0;i<orderdata.orderItems.length;i++){
		orderdata.total_amount += orderdata.orderItems[i].product_price
	}
	
	
	var total_amount_temp = orderdata.total_amount
	//减去积分扣除的钱
	if(orderdata.integral != null && orderdata.integral != 0){
		orderdata.total_amount = orderdata.total_amount - toDecimal(orderdata.integral*0.001)
	}else{
		orderdata.integral = 0
	}
	
	//计算选择的优惠活动
	if(orderdata.couponExchange_id != null && 
		orderdata.couponExchange_price != null &&
		orderdata.couponExchange_deductible != null){
			if(orderdata.total_amount > orderdata.couponExchange_price){
				orderdata.total_amount = orderdata.total_amount - orderdata.couponExchange_deductible
			}
	}
	
	if(orderdata.total_amount <= 0){
		orderdata.total_amount = 0
	}
	
	if(total_amount_temp != orderdata.total_amount){
		$(".price_free").text(total_amount_temp)
		$(".price_free").css("display","block")
	}else{
		$(".price_free").css("display","none")
	}
	
	$("#user_integral_div").text(toDecimal(orderdata.integral*0.001) + "元")
	$("#priceall").text(orderdata.total_amount)
}

//功能：将浮点数四舍五入，取小数点后2位 
function toDecimal(x) { 
  var f = parseFloat(x); 
  if (isNaN(f)) { 
    return; 
  } 
  f = Math.round(x*100)/100; 
  return f; 
}